package com.efounder.aspect;


import com.efounder.aspect.annotation.ExecutionTime;
import com.utilcode.util.LogUtils;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;

import java.lang.reflect.Method;

/**
 * 方法执行时间的切面
 * Created by yqs97.
 * Date: 2019/12/26
 * Time: 11:23
 */
@Aspect
public class ExcuteTimeAspect {

    //    @Pointcut("execution(@com.efounder.aspect.annotation.ExecutionTime * *(..))")
    @Pointcut("@annotation(com.efounder.aspect.annotation.ExecutionTime)")
    public void logPointCut() {
    }

    @Around("logPointCut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        long beginTime = System.currentTimeMillis();
        // 执行方法
        Object object = point.proceed();
        // 执行时长(毫秒)
        long time = System.currentTimeMillis() - beginTime;

        MethodSignature signature = (MethodSignature) point.getSignature();
        Method method = signature.getMethod();
        ExecutionTime excuteTime = method.getAnnotation(ExecutionTime.class);
        String value = "";
        if (excuteTime != null) {
            // 注解上的描述
            value = excuteTime.value();
        }
        // 请求的方法名
        String className = point.getTarget().getClass().getName();
        String methodName = signature.getName();
        LogUtils.e("value=" + value + " time=" + time);
//        LogUtils.e(className +"."+ methodName + value + " time=" + time);

        return object;
    }

}
